const { contextBridge, ipcRenderer } = require('electron')

contextBridge.exposeInMainWorld('versions', {
  node: () => process.versions.node,
  chrome: () => process.versions.chrome,
  electron: () => process.versions.electron,
  ping: () => ipcRenderer.invoke('ping'),
  // 能暴露的不仅仅是函数，我们还可以暴露变量
})

// 主窗口的一些命令
contextBridge.exposeInMainWorld('controlWindow', {
  minWindow: () => ipcRenderer.send('min-window'),
  maxWindow: () => ipcRenderer.send('max-window'),
  closeWindow: () => ipcRenderer.send('close-window'),
})

// 阅读窗口的相关命令
contextBridge.exposeInMainWorld('readingWindow', {
  openWindow: (obj) => ipcRenderer.send('r-open-window', obj),
  minWindow: () => ipcRenderer.send('r-min-window'),
  maxWindow: () => ipcRenderer.send('r-max-window'),
  closeWindow: () => ipcRenderer.send('r-close-window'),
  // 监听要编辑的 harvest 信息
  watchEditHarvest: (callback) =>
    ipcRenderer.on('harvest-info', (_, data) => callback(data)),
})

// 控制本地文件
contextBridge.exposeInMainWorld('localFile', {
  createUserDir: (account) => ipcRenderer.send('f-create-dir', account),
  setUserChatting: (account, receiverId, chatRecord) =>
    ipcRenderer.send('f-set-chatting', { account, receiverId, chatRecord }),
  getUserChatting: (account, receiverId) =>
    ipcRenderer.send('f-get-chatting', { account, receiverId }),
  // 监听传递回来的聊天消息
  sendUserChatting: (callback) =>
    ipcRenderer.on('f-send-chatting', (_, data) => callback(data)),
})
